package com.da.web.sys_menu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.da.web.sys_menu.entity.MakeTree;
import com.da.web.sys_menu.entity.SysMenu;
import com.da.web.sys_menu.mapper.SysMenuMapper;
import com.da.web.sys_menu.service.SysMenuService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author da
 * @time 2023/12/1 下午 4:25
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    public List<SysMenu> getList() {
        //查询出所有的菜单
        QueryWrapper<SysMenu> query = new QueryWrapper<>();
        query.lambda().orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menuList = this.baseMapper.selectList(query);
        //组装树
        List<SysMenu> sysMenus = MakeTree.makeMenuTree(menuList, 0L);
        return sysMenus;
    }

    @Override
    public List<SysMenu> getParentList() {
        //查询类型,只查询类型为目录和菜单的
        String[] types = {"0", "1"};
        //构造查询条件
        QueryWrapper<SysMenu> query = new QueryWrapper<>();
        query.lambda().in(SysMenu::getType, Arrays.asList(types)).orderByAsc(SysMenu::getOrderNum);
        List<SysMenu> menuList = this.baseMapper.selectList(query);
        //构造一个顶级菜单
        SysMenu root = new SysMenu();
        root.setMenuId(0L);
        root.setTitle("顶级菜单");
        root.setParentId(-1L);
        menuList.add(root);
        //组装数据为树形结构
        List<SysMenu> sysMenus = MakeTree.makeMenuTree(menuList, -1L);
        return sysMenus;
    }

    @Override
    public List<SysMenu> getMenuByUserId(Long userId) {
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public List<SysMenu> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getStuMenuByUserId(Long stuId) {
        return this.baseMapper.getStuMenuByUserId(stuId);
    }


}
